<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/7/4
 * Time: 10:41
 */

namespace pay\controllers;



use pay\modules\backend\models\CommonApp;
use yii\web\Controller;

class BasicController extends Controller
{
    public function beforeAction($action)
    {

        $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
        $allow_origin = array(
            'http://10.9.87.28:8080',
            'https://edu.jhscco.com',
            'https://edutest.jhscco.com'
        );
        // P($origin);
        if(in_array($origin, $allow_origin)){
            header('Access-Control-Allow-Origin:'.$origin);
            header('Access-Control-Allow-Methods:POST,GET');
            header('Access-Control-Allow-Credentials:true');
        }

        return true;

    }


    public function actionOpenApp()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $appCommentModel = new CommonApp();

            if(($appInfo = $appCommentModel->addApp($post)) !== false){
                $backInfo = [0, '开通APP实例成功'];//返回信息
                //开通直播间
//                Live::add(time(),$post['app_name'],$appInfo['appid']);
                return $this->showRes($backInfo,['appinfo' => $appInfo]);
            }
        }
        return $this->showRes([0, '开通APP实例失败']);

        //http://paycentre.horsevision.cn/index.php?r=

    }




    protected function getAppId($appId)
    {
        $commontInfo = CommonApp::find()->where('appid = :aid',[':aid' => $appId])->asArray()->one();
        if (!empty($commontInfo)){
            return $commontInfo['app_id'];
        }
        return '';
    }



    /*
返回客户端函数
$args = array(
    '0'  返回码
    '1'  返回信息
)
$data  返回数据，一般为数组，比如获取信息列表信息的时候
$url   返回链接，跳转链接，比如微信登录的时候
*/
    public static function showRes($args = [0, '操作成功'], $data = '', $url = '')
    {
        $res = array(
            'code' => $args[0],
            'info' => $args[1]
        );
        if(!empty($data)){
            $res['data'] = $data;
        }

        if(!empty($url)){
            $res['url'] = $url;
        }
         return json_encode($res, JSON_UNESCAPED_UNICODE); // 中文 生效
//        return json_encode($res,);//JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES = 320  中文/ 同时生效
    }





    protected function getAppIS($appId)
    {
        $commontInfo = CommonApp::find()->where('appid = :aid',[':aid' => $appId])->asArray()->one();
        if (!empty($commontInfo)){
            return $commontInfo['app_id'];
        }
        return '';




    }
}